package com.yituo.mjworld.mvc.form.support.order;

import com.yituo.mjworld.core.annotation.Rules;
import com.yituo.mjworld.core.web.core.BaseForm;
import com.yituo.mjworld.mvc.form.QueryForm;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;


//订单查询功能（日期、订单号、用户ID、是否发货）
public class QueryOrder extends QueryForm {
  /**
   * 按订单查询
   */
  public static final int QUERY_TYPE_BY_ORDERID = 1;
  /**
   * 按日期查询
   */
  public static final int QUERY_TYPE_BY_DATE = 2;
  /**
   * 按用户ID查询
   */
  public static final int QUERY_TYPE_BY_USERID = 3;
  /**
   * 按订单状态查询
   */
  public static final int QUERY_TYPE_BY_STATUS = 4;
  @Rules(fieldName = "日期", required = false)
  public Date theDate;
  @Rules(fieldName = "订单ID", required = false)
  public String orderId;
  @Rules(fieldName = "用户ID", required = false)
  public String userId;
  @Rules(fieldName = "发货状态", required = false)
  public int status;
  @Rules(fieldName = "查询类型")
  public int queryType;

  @Override
  public boolean validate() {
    if (queryType < 1 || queryType > 4) {
      this.mErrorMsg = "查询类型的取值范围越界!";
      return false;
    }

    /*if (queryType == QUERY_TYPE_BY_DATE) {
      if (StringUtils.isEmpty(orderId)) {
        this.mErrorMsg = "订单号不能为空!";
        return false;
      }
      return true;
    }*/

    if (queryType == QUERY_TYPE_BY_ORDERID) {
      if (StringUtils.isEmpty(orderId)) {
        this.mErrorMsg = "订单号不能为空!";
        return false;
      }
      return true;
    }

    if (queryType == QUERY_TYPE_BY_USERID) {
      if (StringUtils.isEmpty(orderId)) {
        this.mErrorMsg = "用户ID不能为空!";
        return false;
      }
      return true;
    }

    if (queryType == QUERY_TYPE_BY_STATUS) {
      if (status < 0 || status > 5) {
        this.mErrorMsg = "查询的状态越界!";
        return false;
      }
      return true;
    }

    return false;
  }

  /**
   * 初始化
   *
   * @return
   */
  @Override
  public boolean init(BaseForm target, Map<String, Object> params) {
    this.setBaseForm(target);
    parse(params);
    try {
      this.page = Integer.parseInt((String) this.getParam().get("page"));
      this.rows = Integer.parseInt((String) this.getParam().get("rows"));
      this.queryType = Integer.parseInt((String) this.getParam().get("queryType"));
      this.status = Integer.parseInt((String) this.getParam().get("status"));
      this.orderId = (String) this.getParam().get("orderId");
      this.userId = (String) this.getParam().get("userId");
      String sTheDate = (String) this.getParam().get("theDate");
      if (!StringUtils.isEmpty(sTheDate)) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        this.theDate = format.parse(sTheDate);
      }
    } catch (Exception e) {
      this.mErrorMsg = "查询类型应该为数字!";
      return false;
    }
    return true;
  }


  public Date getTheDate() {
    return theDate;
  }

  public void setTheDate(Date theDate) {
    this.theDate = theDate;
  }

  public String getOrderId() {
    return orderId;
  }

  public void setOrderId(String orderId) {
    this.orderId = orderId;
  }

  public String getUserId() {
    return userId;
  }

  public void setUserId(String userId) {
    this.userId = userId;
  }

  public int getStatus() {
    return status;
  }

  public void setStatus(int status) {
    this.status = status;
  }

  public int getQueryType() {
    return queryType;
  }

  public void setQueryType(int queryType) {
    this.queryType = queryType;
  }
}
